layui.use(['layer', 'form','table', 'admin', 'ax','ztree'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var $ZTree = layui.ztree;
    var layer = layui.layer;
    var form = layui.form;

    /**
     * 部门岗位关系表管理
     */
    var SysDeptPositions = {
        tableId: "sysDeptPositionsTable",
        condition: {
            deptId: ""
        },
        depname: ""

    };

    /**
     * 初始化表格的列
     */
    SysDeptPositions.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'positionsName', sort: true, title: '岗位名称'},
            {field: 'deptName', sort: true, title: '部门'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };


    SysDeptPositions.onClickDept = function (e, treeId, treeNode) {
        SysDeptPositions.condition.deptId = treeNode.id;
        SysDeptPositions.depname = treeNode.name;
        SysDeptPositions.search();
    };

    /**
     * 点击查询按钮
     */
    SysDeptPositions.search = function () {
        var queryData = {};
        queryData['condition'] = $("#name").val();
        queryData['deptId'] = SysDeptPositions.condition.deptId;
        table.reload(SysDeptPositions.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    SysDeptPositions.btnAssign = function () {
        var deptId = SysDeptPositions.condition.deptId;
        var deptName = SysDeptPositions.depname;
        if (SysDeptPositions.depname != "") {
            layer.open({
                type: 2,
                title: '岗位添加',
                area: ['950px', '550px'],
                offset: '100px',
                anim: 5,
                content: Feng.ctxPath + '/sysDeptPositions/toPostadd?deptId=' + deptId + '&deptname=' + deptName,
                end: function () {
                    table.reload(SysDeptPositions.tableId);
                }
            });
        } else {
            layer.alert('请先选择部门');
        }
    };

    /**
     * 导出excel按钮
     */
    SysDeptPositions.exportExcel = function () {
        var checkRows = table.checkStatus(SysDeptPositions.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    SysDeptPositions.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysDeptPositions/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(SysDeptPositions.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + SysDeptPositions.tableId,
        url: Feng.ctxPath + '/sysDeptPositions/list',
        page: true,
        limit: 30,
        height: "full-98",
        cellMinWidth: 100,
        cols: SysDeptPositions.initColumn()
    });

    var ztree = new $ZTree("deptTree", "/dept/tree");
    ztree.bindOnClick(SysDeptPositions.onClickDept);
    ztree.init();

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        SysDeptPositions.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        SysDeptPositions.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        SysDeptPositions.exportExcel();
    });
    $('#btnAssign').click(function () {
        SysDeptPositions.btnAssign();
    });

    // 工具条点击事件
    table.on('tool(' + SysDeptPositions.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            SysDeptPositions.onDeleteItem(data);
        }
    });
});
